package com.maizhi.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.maizhi.dao.BaseDaoI;
import com.maizhi.httpmodel.EasyuiDataGridJson;
import com.maizhi.httpmodel.EasyuiTreeNode;
import com.maizhi.httpmodel.Json;
import com.maizhi.httpmodel.MenuI;
import com.maizhi.httpmodel.MenuModel;
import com.maizhi.httpmodel.SessionInfo;
import com.maizhi.httpmodel.UserI;
import com.maizhi.model.Menu;
import com.maizhi.model.User;
import com.maizhi.service.BaseServiceI;
import com.maizhi.service.MenuServiceI;
import com.maizhi.service.UserServiceI;
import com.maizhi.util.ResourceUtil;

@Controller
@RequestMapping("/menuController")
public class menuController extends BaseController {

private Logger log = Logger.getLogger(menuController.class);
	
	BaseServiceI<Menu> baseService;
	MenuServiceI menuService;
	BaseDaoI<Menu> baseDao;

	public BaseDaoI<Menu> getBaseDao() {
		return baseDao;
	}

	@Autowired
	public void setBaseDao(BaseDaoI<Menu> baseDao) {
		this.baseDao = baseDao;
	}
 
	public BaseServiceI<Menu> getBaseService() {
		return baseService;
	}

	@Autowired
	public void setBaseService(BaseServiceI<Menu> baseService) {
		this.baseService = baseService;
	}

	public MenuServiceI getMenuService() {
		return menuService;
	}

	@Autowired
	public void setMenuService(MenuServiceI menuService) {
		this.menuService = menuService;
	}
 
	//登录用户
	public UserI getLoginUser(HttpSession session){
		SessionInfo sessionInfo;
		try{
			sessionInfo = (SessionInfo)session.getAttribute(ResourceUtil.getSessionInfoName());
			if(sessionInfo==null){
				return null;
			}
		}catch(Exception e){
			return null;
		}
		return sessionInfo.getUser();
	}
	
	/***
	 * 菜单信息处理模块*******************************************************
	 */	
	
//菜单树 ligerUI
//	@RequestMapping(params = "tree")
//	@ResponseBody
//	public String tree() {
//		List<Menu> list = baseService.find("FROM Menu t");
//		return JSON.toJSONString(list);
//	}
	
	//菜单treeGrid
	@RequestMapping(params = "treeGrid")
	@ResponseBody
	public List<MenuModel> treeGrid(String id) {
		return menuService.treeGrid(id);
	}
	
	/**
	 * 获取菜单树
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(params = "tree")
	@ResponseBody
	public List<EasyuiTreeNode> tree(String id) {
		return menuService.tree(id);
	}
	
	/**
	 * 添加菜单
	 * 
	 * @param menu
	 * @return
	 */
	@RequestMapping(params = "add")
	@ResponseBody
	public Json add(MenuModel menu) {
		Json j = new Json();
		System.out.println("*******************************************"+menu.getSrc());
		
		MenuModel m = menuService.add(menu);
		j.setSuccess(true);
		j.setMsg("添加成功!");
		return j;
	}

	/**
	 * 删除菜单
	 * 
	 * @param menu
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public Json del(MenuModel menu) {
		Json j = new Json();
		menuService.del(menu);
		j.setSuccess(true);
		j.setMsg("删除成功!");
		return j;
	}

	/**
	 * 编辑菜单
	 * 
	 * @param menu
	 * @return
	 */
	@RequestMapping(params = "edit")
	@ResponseBody
	public Json edit(MenuModel menu) {
		Json j = new Json();
		MenuModel m = menuService.edit(menu);
		j.setSuccess(true);
		j.setMsg("编辑成功!");
		return j;
	}
	
	/**
	 * 菜单信息查询
	 * @param session
	 * @return
	 */
	@RequestMapping(params = "menuList")
	public String menuList(HttpSession session) {
		 return "/xtgl/menu_list";
	}
	
	/**
	 * 菜单信息查询
	 * @param session
	 * @return
	 */
	@RequestMapping(params = "menuGrid")
	@ResponseBody
	public EasyuiDataGridJson menuGrid(HttpSession session) {
		 List<Menu> list = baseDao.find("FROM Menu");
		 EasyuiDataGridJson ed = new EasyuiDataGridJson();
		 List<MenuI> mml = new ArrayList<MenuI>();
		 for(Menu m :  list)
		 {
			 MenuI mm = new MenuI();
			 BeanUtils.copyProperties(m,  mm);
			 if(m.getMenu()!=null){
				 mm.setParentId(m.getMenu().getId());
				 mm.setParentText(m.getMenu().getText());
			 }
			 mml.add(mm);
		 }
		 ed.setRows(mml);
		 ed.setTotal((long)list.size());
		 return ed;
	}

}
